<template>
  <a-modal
    style="width: 50%"
    :title="titleMap[mode]"
    :visible="visible"
    :confirm-loading="confirmLoading"
    :destroyOnClose="true"
    @cancel="handleCancel"
    :maskClosable="false">
    <div style="margin-top: 12px;">
      <a-form-model ref="ruleAddForm" :model="addForm" :rules="addRules">
        <a-form-model-item
          label="是否通过"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="passed"
          v-if="mode == 'audit'">
          <a-switch v-model:checked="addForm.passed" />
        </a-form-model-item>
        <a-form-model-item label="描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea :rows="4" :showCount="true" placeholder="请输入描述信息" :max-length="255" />
        </a-form-model-item>

      </a-form-model>
    </div>
    <template slot="footer">
      <a-button key="back" @click="handleCancel">
        关闭
      </a-button>
      <a-button type="primary" :visible="visibleBtn" :loading="loadingBtn" @click="handleOk">
        保存
      </a-button>
    </template>
  </a-modal>
</template>
<script>
import { putBudgetsApproval, deleteBudgets } from '@/api/budget'
export default {
    components: {},
    data () {
        return {
            addRules: {
                passed: [
                    { required: true, message: '是否通过!', trigger: 'change' }
                ]
            },
            addForm: {
                budgetId: '',
                description: '',
                passed: false
            },
            //  表单样式
            labelCol: {
                xs: { span: 24 },
                sm: { span: 5 }
            },
            wrapperCol: {
                xs: { span: 24 },
                sm: { span: 16 }
            },
            visible: false,
            confirmLoading: false,
            loading: false,
            visibleBtn: false,
            loadingBtn: false,
            mode: 'audit',
            titleMap: {
                audit: '审核预算',
                del: '删除预算'
            },
            rowData: ''
        }
    },
    methods: {
        // 显示
        open (mode = 'audit') {
            this.mode = mode
            this.visible = true
            return this
        },
        // 获取行数据和id
        setData (row) {
            this.rowData = row
            if (row.state == 4) {
                this.addForm.passed = true
            } else {
                this.addForm.passed = false
            }
            this.addForm.budgetId = row.budgetId
        },
        // 关闭
        handleCancel () {
            this.visible = false
            this.loadingBtn = false
            this.visibleBtn = false
            this.$emit('close')
        },
        // 确认
        handleOk () {
            this.loadingBtn = true
            this.visibleBtn = true
            this.$refs.ruleAddForm.validate(valid => {
                if (valid) {
                    const { budgetId, description } = this.addForm

                    const par = {
                        budgetId,
                        description
                    }
                    if (this.mode == 'audit') par.passed = this.addForm.passed ? 1 : 0
                    const Api = this.mode == 'audit' ? putBudgetsApproval : deleteBudgets
                    Api(par).then(res => {
                        if (res.code === 200) {
                            this.$notification['success']({
                                message: '提示',
                                description: '保存成功！',
                                duration: 8
                            })
                            this.visible = false
                            this.$emit('success')
                        } else {
                            this.$notification['error']({
                                message: '提示',
                                description: res.message,
                                duration: 8
                            })
                        }
                        this.visibleBtn = false
                        this.loadingBtn = false
                    })
                } else {
                    this.loadingBtn = false
                    this.visibleBtn = false
                    return false
                }
            })
        }
    }
}
</script>
<style scoped lang="less" ></style>
